<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bubbleSort</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const bubbleSort = arr => {
            let swapped = false;
            let a = [...arr];
            for (let i = 0; i < a.length; i++) {
                swapped = false;
                for (let j = 0; j < a.length - i; j++) {
                    if (a[j + 1] < a[j]) {
                        //数组解构的方式
                        [a[j], a[j + 1]] = [a[j + 1], a[j]];
                        swapped = true;
                    }
                }
                if (!swapped) {
                    return a;
                }
            }
            return a;
        };
        console.log(bubbleSort([2, 1, 4, 3]));
    </script>
</body>

</html>